﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionEngine
{
    /// <summary>
    /// 解析辅助工具
    /// </summary>
    public class ParserUtil
    {
        internal static Variable getValue(string value)
        {
            if (TableManager.ExistVariable(value))
            {
                return TableManager.GetVariable(value);
            }
            else if (TableManager.bNumarical(value))
            {
                Variable vari = new Variable();
                vari.Name = value;
                int varIntValue = 0;
                float varFloatValue = 0;
                double varDoubleValue = 0;
                if (int.TryParse(value, out varIntValue))
                {
                    vari.Value = varIntValue;
                    vari.ValueType = DataType.INT;
                }
                else if (float.TryParse(value, out varFloatValue))
                {
                    vari.Value = varFloatValue;
                    vari.ValueType = DataType.FLOAT;
                }
                else if (double.TryParse(value, out varDoubleValue))
                {
                    vari.Value = varDoubleValue;
                    vari.ValueType = DataType.DOUBLE;
                }
                else
                    throw new Exception("找不到指定的数值类型");

                return vari;
            }
            else
            {
                return new Variable() { Value = value, ValueType = DataType.STRING, BBuiltIn = false };
            }
        }
    }
}
